package com.hdu.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hdu.entity.Divide;
import com.hdu.entity.Subsection;
import com.hdu.mapper.DivideMapper;
import com.hdu.mapper.SubsectionMapper;
import com.hdu.pojo.DataCell;
import com.hdu.pojo.PageBean;
import com.hdu.service.ToolService;
import com.hdu.vo.SubsectionChart;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 工具实体类 服务实现类
 * </p>
 *
 * @author xiaobai
 * @since 2023-11-02
 */
@Service
@RequiredArgsConstructor
public class ToolServiceImpl implements ToolService {

    private final SubsectionMapper subsectionMapper;

    private final DivideMapper divideMapper;


    @Override
    public PageBean<Subsection> subsection(Integer current, Integer size, Integer region, String year, Integer type, String score) {

        Page<Subsection> page = new Page<>(current, size);

        QueryWrapper<Subsection> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotEmpty(region.toString()), "region", region)
                .eq(StrUtil.isNotEmpty(type.toString()), "type", type)
                .eq(StrUtil.isNotEmpty(year), "year", year)
                .eq(StrUtil.isNotEmpty(score), "score", score);

        IPage<Subsection> iPage = subsectionMapper.selectPage(page, wrapper);

        return new PageBean<>(iPage.getTotal(), iPage.getRecords());
    }

    @Override
    public List<SubsectionChart> chart(Integer region, String year, Integer type) {

        List<SubsectionChart> chartList = new ArrayList<>();

        String[] batch = {"本科一批", "本科二批", "本科批", "专科批"};
        Map<String, Integer> score = new HashMap<>();
        for (String s : batch) {
            QueryWrapper<Divide> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("region", region).eq("year", year).eq("type", type);
            Divide divide = divideMapper.selectOne(queryWrapper.eq("batch", s));
            if (divide != null) {
                score.put(s, divide.getScore());
            }
        }
        QueryWrapper<Subsection> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotEmpty(region.toString()), "region", region)
                .eq(StrUtil.isNotEmpty(type.toString()), "type", type)
                .eq(StrUtil.isNotEmpty(year), "year", year);

        List<Subsection> subsections = subsectionMapper.selectList(wrapper);
        for (Subsection subsection : subsections) {
            SubsectionChart subsectionChart = new SubsectionChart();
            subsectionChart.setScore(subsection.getScore());
            subsectionChart.setNumber(subsection.getNumber());
            List<DataCell> cellList = new ArrayList<>();
            for (int i = 2023; i > 2019; i--) {
                if (!year.equals(String.valueOf(i))) {
                    int temp = type == 2020 ? type + 2 : (i == 2020 ? type - 2 : type);
                    wrapper = new QueryWrapper<>();
                    wrapper.eq("region", region).eq("type", temp).eq("year", i)
                            .gt("total", subsection.getTotal() - subsection.getNumber());
                    Subsection sub = subsectionMapper.selectOne(wrapper.last("limit 1"));
                    if (sub != null) {
                        cellList.add(DataCell.of(i, sub.getScore()));
                    } else {
                        cellList.add(DataCell.of(i, "0-99"));
                    }
                }
            }
            subsectionChart.setCellList(cellList);
            String keySet = "";
            for (String s : score.keySet()) {
                if (subsection.getScore().compareTo(String.valueOf(score.get(s))) >= 0) {
                    subsectionChart.setBatch(DataCell.of(s, score.get(s)));
                    break;
                }
                keySet = s;
            }
            if (subsectionChart.getBatch() == null) {
                subsectionChart.setBatch(DataCell.of(keySet, score.get(keySet)));
            }
            subsectionChart.setSection((subsection.getTotal() - subsection.getNumber() + 1) + "-" + subsection.getTotal());
            chartList.add(subsectionChart);
        }
        return chartList;
    }

    @Override
    public PageBean<Divide> divide(Integer current, Integer size, Integer region, String year, Integer type) {
        Page<Divide> page = new Page<>(current, size);

        QueryWrapper<Divide> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotEmpty(region.toString()), "region", region)
                .eq(type != 0, "type", type)
                .eq(StrUtil.isNotEmpty(year), "year", year);

        IPage<Divide> iPage = divideMapper.selectPage(page, wrapper);

        return new PageBean<>(iPage.getTotal(), iPage.getRecords());
    }
}
